package com.power.sifa.approve.executor.impl;

import com.alibaba.fastjson.JSON;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.power.flowable.api.IPersonalFeign;
import com.power.sifa.util.executor.AbstractPostExecutor;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.St_admreview;
import com.power.sifa.approve.model.enumeration.Tp_proctype;
import com.power.sifa.approve.model.enumeration.Tp_proctypeEnum;
import com.power.sifa.approve.model.pojo.vo.ProcessVariablesVo;
import com.power.sifa.approve.service.Apl_admre_auditService;
import com.power.sifa.approve.service.Apl_admreviewService;
import com.power.sifa.approve.service.Apl_mainService;
import com.power.sifa.approve.service.BusinessApproveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 停止执行完成服务
 */
@Component
@Slf4j
public class HearAuditStop extends AbstractPostExecutor {

    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_admreviewService apl_admreviewService;
    @Resource
    private BusinessApproveService businessApproveService;
    @Resource
    private Apl_admre_auditService apl_admre_auditService;
    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private IPersonalFeign iPersonalFeign;

    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> execute() throws Exception {

        Apl_admre_audit auditObj = this.apl_admre_auditService.getById(super.applyid);

        // 2.1  如果需要市领导审批, 不自动生成审批表
        if (YesNo.NO == auditObj.getIssecondout()) {
            // 2. 生成审批表
            this.businessApproveService.createThreeLevelApprove(super.applyid, this.userid, this.username, Tp_proctype.HEAR_STOP);
            if (YesNo.YES == auditObj.getIscitymanager()) {
                Apl_main mainObj = this.apl_mainService.getById(super.applyid);
                Apl_main faMainObj = this.apl_mainService.getById(auditObj.getApplyid());
                // 3.1  启动一个结果审批流程 (不管要不要市领导审批都需要一个结果审批流程)
                Apl_admreview admreObj = this.apl_admreviewService.getById(auditObj.getApplyid());
                Apl_main newMainObj = JSON.parseObject(JSON.toJSONString(mainObj), Apl_main.class);
                Date now = new Date();
                newMainObj.setId(now.getTime() + NanoIdUtils.randomNanoId());
                String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
                newMainObj.setCreatetime(date);
                newMainObj.setCreatetime(date);
                String soninstid = this.businessApproveService.createSonProcess(faMainObj, newMainObj, admreObj, this.userid, "", St_admreview.ACCEPT, Tp_proctype.HEAR_MEDIATE, auditObj.getIscitymanager(), YesNo.YES);
                // 记录流程变量
                Map<String, Object> variableMap = new HashMap<>(16);
                variableMap.put(ProcessVariablesVo.CITYAPPROVE, auditObj.getIssecondout());
                ResponseDTO res = this.iPersonalFeign.saveVariable(soninstid, variableMap);
                if (0 != res.getCode()) {
                    throw new RuntimeException(res.getMsg());
                }
                return null;
            }
        }

        // 2. 删除一个子状态
        Apl_admreview admreObj = this.apl_admreviewService.getById(auditObj.getApplyid());
        String sonStatus = this.businessApproveService.delSonStatus(admreObj.getSonstatus(), Tp_proctypeEnum.HEAR_STOP.getStatus());
        this.apl_admreviewService.updatesSonStatus(admreObj.getId(), sonStatus);

        // 刷新文书日期
        this.businessApproveService.processEndedUpdatePreviewDoc(auditObj.getApplyid(), Tp_proctype.HEAR_STOP, null);
        // 文书盖章
        this.businessApproveService.templateStamp(auditObj.getApplyid(), Tp_proctype.HEAR_STOP);
        return null;
    }

}
